package com.aaa.jdbc.userinfo.impl;

import com.aaa.java15.Person;
import com.aaa.jdbc.userinfo.UserInfo;
import com.aaa.jdbc.userinfo.UserInfoDao;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/7/28 9:22
 * @description：将数据保存到文件中
 * @modified By：
 * @version: 1.0
 */
public class UserInfoDaoFileImp  implements UserInfoDao {
    @Override
    public boolean saveUserInfo(UserInfo userInfo) {
        File file = new File("D:\\qy156\\userinfo.txt");
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(userInfo);
            objectOutputStream.flush();
            objectOutputStream.close();
            fileOutputStream.close();
            return  true;
        } catch (Exception e) {
            System.out.println("保存失败");
            return false;
        }

    }

    @Override
    public boolean deleteUserInfoById(Integer id) {
        return false;
    }

    @Override
    public int saveUserInfoKey(UserInfo userInfo) {
        return 0;
    }

    @Override
    public List<Map<String, Object>> queryAll() {
        //从文件中读取userinfo对象，将此对象放到map中，
        try {
            File file = new File("D:\\qy156\\userinfo.txt");
            FileInputStream fileInputStream = new FileInputStream(file);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            UserInfo object = (UserInfo) objectInputStream.readObject();
            Map<String, Object> map = new HashMap<>();
            map.put("id",object.getId());
            map.put("username",object.getUsername());
            map.put("password",object.getPassword());
            map.put("createTime",object.getCreateTime());
            map.put("phone",object.getPhone());
            List<Map<String, Object>> list = new ArrayList<>();
            list.add(map);
            return list;
        }catch (Exception e){
            System.out.println("查询失败");
        }

        return null;
    }

    @Override
    public List<Map<String, Object>> queryAllByCondition(Map<String, Object> condition) {
        return null;
    }
}
